import {dateFormat, toast} from '../../utils/util.js'
// 根据需求定制，无通用性，因为从原版日历组件修改而逻辑不同，所以全是垃圾代码= =
Component({
    data: {
        monthCalendar: [],
        todayMonth: '',
        today: 0,
        select: {},
        isWeek: true,
        current: 0,
        enable: {},
        disables: {}
    },
    properties: {
        todo: Object,
        date: String,
        disable: Object,
        dateStart: String,
        dateEnd: String,
        isRadio: Boolean
    },
    observers: {
        'disable': function (newdis) {
            console.log(newdis)
            this.setData({
                disables: newdis
            })
            this.init()
        },
        'date': function (date) {
            if(date){
                this.selectTime(date)
            }
            else{
                this.getMonthArray()
            }
        },
    },
    created(){
    },
    methods: {
        init(){
            this.initEnable()
            this.getMonthArray()
            this.userSelect({
                detail:{
                    value: dateFormat('YYYY-mm', new Date())
                }
            })
        },
        initEnable(){
            const enable = {}
            let dateStart = new Date(this.data.dateStart)
            const dateEnd = new Date(new Date(this.data.dateEnd).setDate(new Date(this.data.dateEnd).getDate()+1))
            if(dateStart.getTime() < new Date().getTime()){
                dateStart = new Date(new Date().getTime())
            }
            if(dateStart.getTime() > dateEnd.getTime()){
                toast('暂无可选日期')
                return 
            }
            // 生成可选列表
            while(dateStart.getTime() <= dateEnd.getTime()){
                const date = dateFormat('YYYY-mm-dd',dateStart)
                if(!this.data.disables || !this.data.disables[date]){
                    enable[date]= true
                }
                dateStart = new Date(dateStart.setDate(dateStart.getDate() + 1))
            }
            this.setData({
                enable: enable
            })
        },
        getMonthArray(date = new Date().getTime()) {
            const monthNum = 24
            // 获取最近一年的日历
            this.setData({
                'monthCalendar':  this.getMonth(new Date(date), monthNum)
            })
        },
        getToday() {
            let date = new Date(new Date().setHours(0, 0, 0, 0))
            // 设置今日时间
            this.setData({
                today: date.getTime()
            })
        },
        // selectDay 生成月列表
        getMonth(selectDay, monthNum) {
            selectDay = dateFormat('YYYY-mm', selectDay)
            // 当月第一天日期
            let date = new Date(selectDay)
            date =new Date(date.setDate(date.getDate()-date.getDay()))
            let dateTime = date.getTime()
            // 每天的毫秒数
            const dayTime = 1000 * 60 * 60 * 24
            // 本月结果数组
            const calendars = []

            let num = 0
            for (let i = 0; i < 5; i++) {
                const calendar = []
                // calendar.push(...(new Array(new Date(dateTime + (num * dayTime)).getDay())))
                for (let j = 0; j < 7; j++) {
                    const day = dateTime + (num * dayTime)
                    const date2 = new Date(day)
                    
                    if(j !== 0 && date2.getDay() === 0){
                        break
                    }
                    
                    calendar.push({
                        day: date2.getDate(),
                        date: this.getYearMonth(date2),
                        time: date2.getTime(),
                        fulldate: dateFormat('YYYY-mm-dd', date2)
                    })
                    num += 1
                }
                calendars.push(calendar)
            }
            if(monthNum === 0){
                return [calendars]
            }
            const result = [calendars]
            const nextDate = new Date(selectDay)
            result.push(...this.getMonth(new Date(nextDate.setMonth(nextDate.getMonth() + 1)), monthNum - 1))
            return result
        },

        getYearMonth(time) {
            return time.getFullYear() + '-' + (time.getMonth() < 9 ? ('0' + (time.getMonth() + 1)) : (time.getMonth() + 1))
        },
        upCalendarSelectDay(){
            this.getMonthArray(new Date())
        },
        userSelect(event){
            for(let current = 0; current < this.data.monthCalendar.length ; current++){
                if(this.data.monthCalendar[current][2][0].date ===  event.detail.value){
                    this.setData({
                        todayMonth: event.detail.value,
                        current
                    })
                    return 
                }
            }

        },
        selectTime(event) {
            const time = event.currentTarget ? event.currentTarget.dataset.fulldate : event
            if(!this.data.enable[time]){
                return
            }
            if(this.data.isRadio){
                this.data.select = {}
                this.data.select[time] = true
            }
            else{
                if(this.data.select[time]){
                    delete this.data.select[time]
                }
                else{
                    this.data.select[time] = true
                }
            }
            this.setData({
                select: this.data.select
            })
        },
        // 日期切换，通知父组件更新todo
        onMonthSwitch(event){
            let date = this.data.monthCalendar[event.detail.current][3][0].fulldate
            this.setData({
                todayMonth: date.slice(0,7)
            })
        },
        onSubmit(){
            this.triggerEvent('selected', Object.keys(this.data.select))
        },
    }
})